//
//  WTALeftViewController.m
//  WTAZoomNavigationController
//
//  Created by Andrew Carter on 11/13/13.
//  Copyright (c) 2013 Andrew Carter. All rights reserved.
//

#import "WTALeftViewController.h"
#import "GoodViewController.h"
#import "WTAZoomNavigationController.h"
#import "ImageViewController.h"
#import "CompanyViewController.h"
#import "BrandViewController.h"
#import "DesignerViewController.h"
#import "MyViewController.h"
NSString * const WTATableCellIdentifier = @"WTATableCellIdentifier";

@interface WTALeftViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *datasource;
@property (nonatomic, strong) NSArray *images;

@property (nonatomic, assign) BOOL didSelectInitialViewController;

@end

@implementation WTALeftViewController

#pragma mark - UIViewController Overrides

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        [self setDatasource:@[@"精选推荐", @"家居图库", @"家装公司", @"品牌展厅", @"设计师", @"我的收藏"]];
        self.images = @[[UIImage imageNamed:@"精选推荐"], [UIImage imageNamed:@"家具图库"], [UIImage imageNamed:@"家装公司"], [UIImage imageNamed:@"全部品牌"], [UIImage imageNamed:@"设计师"], [UIImage imageNamed:@"我的收藏"]];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:WTATableCellIdentifier];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    if (![self didSelectInitialViewController])
    {
        [self setDidSelectInitialViewController:YES];
        [self tableView:self.tableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    }
}


- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

#pragma mark - UITableViewDatasource Methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datasource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:WTATableCellIdentifier forIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor clearColor]];
//    [cell.textLabel setTextColor:[UIColor whiteColor]];
    [cell.textLabel setText:self.datasource[indexPath.row]];
    [cell setSelectedBackgroundView:[UIView new]];
    [cell.textLabel setHighlightedTextColor:[UIColor purpleColor]];
    cell.imageView.image = self.images[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:0];

#pragma mark - 初始化子控制器
    
    if (0 == indexPath.row) {
        GoodViewController *good = [[GoodViewController alloc] initWithNibName:@"GoodViewController" bundle:nil];
        [good.navigationItem setTitle:self.datasource[indexPath.row]];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:good];
        [[self wta_zoomNavigationController] setContentViewController:navigationController];
        [[self wta_zoomNavigationController] hideLeftViewController:YES];

    } else if (1 == indexPath.row){
        ImageViewController *image = [[ImageViewController alloc] initWithNibName:@"ImageViewController" bundle:nil];
        [[image navigationItem] setTitle:[self datasource][[indexPath row]]];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:image];
        [[self wta_zoomNavigationController] setContentViewController:navigationController];
        [[self wta_zoomNavigationController] hideLeftViewController:YES];
    } else if (2 == indexPath.row){
        CompanyViewController *company = [[CompanyViewController alloc] initWithNibName:@"CompanyViewController" bundle:nil];
        [[company navigationItem] setTitle:[self datasource][[indexPath row]]];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:company];
        [[self wta_zoomNavigationController] setContentViewController:navigationController];
        [[self wta_zoomNavigationController] hideLeftViewController:YES];
    } else if (3 == indexPath.row){
        BrandViewController *brand = [[BrandViewController alloc] initWithNibName:@"BrandViewController" bundle:nil];
        [[brand navigationItem] setTitle:[self datasource][[indexPath row]]];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:brand];
        [[self wta_zoomNavigationController] setContentViewController:navigationController];
        [[self wta_zoomNavigationController] hideLeftViewController:YES];
    } else if (4 == indexPath.row){
        DesignerViewController *designer = [[DesignerViewController alloc] initWithNibName:@"DesignerViewController" bundle:nil];
        [[designer navigationItem] setTitle:[self datasource][[indexPath row]]];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:designer];
        [[self wta_zoomNavigationController] setContentViewController:navigationController];
        [[self wta_zoomNavigationController] hideLeftViewController:YES];
    } else if (5 == indexPath.row){
        MyViewController *my = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
        [[my navigationItem] setTitle:[self datasource][[indexPath row]]];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:my];
        [[self wta_zoomNavigationController] setContentViewController:navigationController];
        [[self wta_zoomNavigationController] hideLeftViewController:YES];
    }
}
@end
